package leetcode.part3;

import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

public class S49 {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String,List<String>> map=new HashMap<>();
        for (String s : strs) {
            String[] split = s.split("");
            Arrays.sort(split);
            StringBuilder sb=new StringBuilder();
            for (String string : split) {
                sb.append(string);
            }
            String key= sb.toString();
            map.computeIfAbsent(key,k->new ArrayList<>());
            map.get(key).add(s);
        }
        List<List<String>> res=new ArrayList<>();
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            res.add(entry.getValue());
        }
        return res;
    }

    @Test
    public void test() {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        System.out.println(groupAnagrams(strs));
    }
}
